#!/usr/bin/python

from lxml import etree
from StringIO import StringIO

def loadFile(name,defaultRoot="html"):
    try: 
        parser=etree.XMLParser(remove_blank_text=True)
        file=open(name, "r")
        elTree = etree.parse(StringIO(file.read()),parser)
        file.close()
        tree = elTree.getroot()
    except Exception as Ex1:
        print("File not loaded: "+str(type(Ex1)))
        tree = etree.Element(defaultRoot)
        title = etree.SubElement(tree,"fileID")
        title.text = defaultRoot
        saveFile(name, tree)
    # end Try
    return tree
# end loadFile

def saveFile(name, tree):
    try:
        file=open(name,"w")
        file.write(etree.tostring(tree, pretty_print=True))
        file.close()
    except Exception as Ex1:
        print("File save failed: "+str(type(Ex1)))
        print Ex1

